import datetime
import json
import os
from enum import Enum
import random
import time
import requests
import hashlib


def get_nonce():
    """
        生成一个指定长度的随机字符串
        """
    randomlength = 16
    random_str = ''
    base_str = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678'
    length = len(base_str) - 1
    for i in range(randomlength):
        random_str += base_str[random.randint(0, length)]
    # print(random_str)
    return random_str


def signIn(data,index):
    url = 'https://bt-h5-gateway.beantechyun.com/app-api/api/v1.0/signIn/sign'
    headers = {
        'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 15_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 /sa-sdk-ios fromappios haval',
        'brand': '1',
        'Pragma': 'no-cache',
        'cVer': '5.0.100',
        'accessToken': data,
        'os': 'IOS',
        'rs': '2',
        'terminal': 'GW_APP_Haval',
        'If-Modified-Since': '0',
        'Accept-Language': 'zh-CN,zh-Hans;q=0.9',
        'Accept': 'application/json, text/plain, */* ',
        'Content-Type': 'application/json;charset=utf-8'
    }
    data = {'port': 'HJ0002'}
    response = requests.post(url=url, data=json.dumps(data), headers=headers).json()
    print(f'第{index}个账号 签到返回: {response}')
    return f'第{index}个账号 签到返回: {response}'


def webhook(message, webhookToken):
    url = f'http://www.pushplus.plus/send'
    headers = {
        'Content-Type': 'application/json'
    }
    data = {
        "token": webhookToken,
        "title": "哈佛",
        "content": message,
        "template": "json"
    }
    response = requests.post(url=url, data=json.dumps(data), headers=headers).json()


def sleepmoment():
    random_seconds = random.randint(1, 10)
    print(f'休眠{random_seconds}秒')
    time.sleep(random_seconds)


if __name__ == '__main__':
    print('开始执行...')
    sleepmoment()
    cks = os.environ['hfzj']
    cks = cks.split('@')
    webhookToken = os.environ['PUSH_PLUS_TOKEN']
    msg = ""
    for index, ck in enumerate(cks):
        msg += signIn(ck, index)

    webhook(msg, webhookToken)
